﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Globalization;

namespace CloudReader.Update.Client
{
    public static class Window
    {
        class WindowMaxSizeButton : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var ws = (System.Windows.WindowState)value;
                return ws == System.Windows.WindowState.Maximized ? "2" : "";
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
        class ListVisibility : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var enabled = (bool)value;
                if(enabled)
                {
                    return System.Windows.Visibility.Visible;
                }
                else
                {
                    return System.Windows.Visibility.Collapsed;
                }
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
        class WindowAction : ICommand
        {
            Action<object> _exe;
            public WindowAction(Action<object> exe)
            {
                _exe = exe;
                CanExecuteChanged += WindowAction_CanExecuteChanged;
            }

            private void WindowAction_CanExecuteChanged(object sender, EventArgs e)
            {
                CanExecute(sender);
            }

            public event EventHandler CanExecuteChanged;

            public bool CanExecute(object parameter)
            {
                return true;
            }

            public void Execute(object parameter)
            {
                _exe(parameter);
            }
        }

        public static ICommand CloseAction { get; set; } = new WindowAction((param) => {
            if (param is System.Windows.Window)
                (param as System.Windows.Window).Close();
        });
        public static ICommand MinSizeAction { get; set; } = new WindowAction((param) => {
            if (param is System.Windows.Window)
                (param as System.Windows.Window).WindowState = System.Windows.WindowState.Minimized;
        });
        public static ICommand MaxSizeAction { get; set; } = new WindowAction((param) => {
            if (param is System.Windows.Window)
            {
                var p = param as System.Windows.Window;
                if(p.WindowState == System.Windows.WindowState.Maximized)
                {
                    p.WindowState = System.Windows.WindowState.Normal;
                }
                else
                {
                    p.WindowState = System.Windows.WindowState.Maximized;
                }
            }
        });

        public static IValueConverter WindowsState2Content { get; set; } = new WindowMaxSizeButton();
        public static IValueConverter Enabled2Visibility { get; set; } = new ListVisibility();
    }
}
